package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasAppEntity;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.model.PaasAppModel;
import com.xbongbong.paas.model.PaasMenuModel;
import com.xbongbong.paas.script.pojo.dto.OnlineEditorOpenDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.AccessLinkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.menu.enums.SystemAppMenuEnum;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.sys.domain.entity.CompanyConfigEntity;
import com.xbongbong.sys.model.CompanyConfigModel;
import com.xbongbong.sys.model.CompanyModel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author: yingjf
 * @date: 2023/3/9 09:47
 * @description: wps在线编辑器  相关脚本接口
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/onlineEditor")
public class OnlineEditorScriptController {
    private ProBaseConfig proBaseConfig;
    private CompanyModel companyModel;
    private PaasMenuModel paasMenuModel;
    private PaasAppModel paasAppModel;
    private CompanyConfigModel companyConfigModel;

    /**
     * wps在线编辑开通    commonhelp.isOpenOnlineEditor();
     * date: 2023/3/9 18:52
     * @author yingjf
     * @param onlineEditorOpenDTO
     * @param br
     */
    @RequestMapping(value = "/open", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String open(@RequestBody @Valid OnlineEditorOpenDTO onlineEditorOpenDTO, BindingResult br) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(onlineEditorOpenDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        String corpid = onlineEditorOpenDTO.getCorpid();
        if (corpid == null) {
            return "corpid为空！";
        }

        PaasAppEntity paasAppEntity = null;
        paasAppEntity = paasAppModel.getByAlias(SystemAppMenuEnum.CONTRACT_TEMPLATE_BASE.getAlias(), corpid);
        if (paasAppEntity == null) {
            paasAppEntity = new PaasAppEntity();
            paasAppEntity.setCorpid(corpid);
            paasAppEntity.setEnable(1);
            paasAppEntity.setAlias(SystemAppMenuEnum.CONTRACT_TEMPLATE_BASE.getAlias());
            paasAppEntity.setSaasMark(SaasMarkEnum.SAAS.getCode());
            paasAppEntity.setName(SystemAppMenuEnum.CONTRACT_TEMPLATE_BASE.getName());
            paasAppEntity.setSort(getAppMaxSort(corpid));
            paasAppEntity.setDel(0);
            paasAppEntity.setIcon(AccessLinkEnum.CONTRACT_TEMPLATE_BASE.getIcon());
            paasAppEntity.setColor(AccessLinkEnum.CONTRACT_TEMPLATE_BASE.getColor());
            paasAppEntity.setCreatorId("-1");
            paasAppModel.insert(paasAppEntity);
        } else {
            log.warn("菜单存在不需插入");
        }

        CompanyConfigEntity companyConfigEntity = companyConfigModel.getByConfigAlias(CompanyConfigEnum.ONLINE_EDITOR_WHITE.getAlias(), corpid);
        if(companyConfigEntity==null){
            // 插入redis公司配置
            companyConfigModel.save(corpid, CompanyConfigEnum.ONLINE_EDITOR_WHITE.getAlias(), CompanyConfigEnum.ONLINE_EDITOR_WHITE.getName(), CompanyConfigEnum.ONLINE_EDITOR_WHITE.getValue());
        }else{
            log.warn("公司配置表存在不需插入");
        }
        return "开通成功";
    }


    public Integer getAppMaxSort(String corpid) throws XbbException {
        Integer sort;
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("columns", "id, sort");
        param.put("corpid", corpid);
        param.put("orderByStr", "sort desc");
        param.put("del", DelEnum.NORMAL.getDel());
        List<PaasAppEntity> appList = paasAppModel.findEntitys(param);
        if (Objects.isNull(appList)) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.APP_NOT_EXIST);
        }
        PaasAppEntity paasAppEntity = appList.get(0);
        Integer count = appList.size();
        if (paasAppEntity.getSort() > count) {
            sort = paasAppEntity.getSort();
        } else {
            sort = count;
        }
        return sort;
    }

}
